package com.kygeng.datasearch;


import com.kygeng.common.model.database.CompanyInfo;
import com.kygeng.common.model.document.CompanyInfoDoc;
import com.kygeng.datasearch.mapper.CompanyInfoEsRepository;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: GengKY
 * @Date: 2023/7/1 20:55
 */
@SpringBootTest
public class CompanyInfoUtil {


    @Autowired
    private CompanyInfoEsRepository companyInfoEsRepository;

    @Autowired
    RestHighLevelClient restHighLevelClient;




    @Test
    void getById() {

        Optional<CompanyInfoDoc> byId = companyInfoEsRepository.findById(String.valueOf(10));

        System.out.println(byId.get());

    }

    /**
     * 测试查询
     */
    @Test
    public void testSelect() throws IOException {

        //创建搜索请求
        SearchRequest searchRequest = new SearchRequest("company_info");
        //创建搜索对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.termsQuery("companyName", "erntogra"))
                .sort("incorporationDate", SortOrder.DESC)
                .from(0)
                .size(10)
                .highlighter(new HighlightBuilder()
                        .field("*")
                        .requireFieldMatch(false)
                        .preTags("<span style='color:red'>")
                        .postTags("</span>"));

        searchRequest.source(searchSourceBuilder);

        //执行搜索
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        SearchHit[] searchHits = searchResponse.getHits().getHits();

        for (SearchHit hit : searchHits) {

            System.out.println(hit);
            //原始文档 ===自己封装===》 Emp对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();

            System.out.println(sourceAsMap);

            //高亮字段
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            if (highlightFields.containsKey("companyName")) {
                String string = highlightFields.get("companyName").fragments()[0].toString();
                System.out.println(string);
            }
        }
    }


    @Test
    void insertEsCompanyInfo() {


    }

    @Test
    void insertEsCompanyInfoOne() {

        CompanyInfoDoc doc = new CompanyInfoDoc();

        doc.setId(10096);
        doc.setCompanyCode("11100010096");
        doc.setCompanyName("Erntogra Associates");
        doc.setTags(Arrays.asList("专精特新中小企业", "科技型小巨人企业", "中华老字号", "小巨人企业"));

        companyInfoEsRepository.save(doc);
    }
}
